package com.suixuan.support.util.json;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.util.Assert;

import com.suixuan.support.log.LogConstant;
import com.suixuan.support.log.LogFactory;
import com.suixuan.support.util.web.vo.ErrorVo;

import flexjson.JSONSerializer;

/**
 * Nanjing Zhou Su co.,Ltd
 * 
 * @Title: AjaxJsonResponse.java
 * @Package: com.suixuan.spring.util.web
 * @Description: AjaxJson返回操作的类
 * @Author: 束剑
 * @Date 2010-3-15 上午11:55:26
 * @Version V1.0
 */
public final class AjaxJsonResponse {
	/**
	 * 操作是否成功(例如保存操作等)
	 */
	public static final String OPERATION_SUCCESSFUL = "successful";

	/**
	 * 失败
	 */
	public static final String ERROR = "error";

	public static final String VALIDATED = "validated";

	private static String defContentType = "text/Xml;charset=utf-8";

	private HttpServletResponse response;

	private Map<String, Object> attributeDatas;

	private String contentType = defContentType;

	public AjaxJsonResponse() {
		super();
	}

	/**
	 * 带response构造
	 * 
	 * @param response
	 *            响应
	 */
	public AjaxJsonResponse(HttpServletResponse response) {
		super();
		this.response = response;

		this.attributeDatas = new HashMap<String, Object>();

		this.attributeDatas.put(OPERATION_SUCCESSFUL, false);

	}

	/**
	 * @param response
	 *            响应
	 * @param attributeDatas
	 *            返回结果值
	 */
	public AjaxJsonResponse(HttpServletResponse response, Map<String, Object> attributeDatas) {
		super();

		Assert.notNull(response);

		Assert.notNull(attributeDatas);

		this.response = response;

		this.attributeDatas = attributeDatas;
	}

	/**
	 * 全参构造
	 * 
	 * @param response
	 *            响应
	 * @param attributeDatas
	 *            返回结果值
	 * @param contentType
	 */
	public AjaxJsonResponse(HttpServletResponse response, Map<String, Object> attributeDatas, String contentType) {
		super();
		this.response = response;
		this.attributeDatas = attributeDatas;
		this.contentType = contentType;
	}

	/**
	 * 设置是否操作成功
	 * 
	 * @param seccessful
	 *            是否成功
	 */
	@Deprecated
	public void setSuccessful(boolean seccessful) {

		Assert.notNull(attributeDatas);

		this.attributeDatas.put(OPERATION_SUCCESSFUL, seccessful);
	}

	/**
	 * 设置是否操作成功
	 * 
	 */
	public void setSuccessful() {

		Assert.notNull(attributeDatas);

		this.attributeDatas.put(OPERATION_SUCCESSFUL, true);
	}

	/**
	 * 添加返回结果
	 * 
	 * @param key
	 *            键
	 * @param data
	 *            值
	 */
	public void putData(String key, Object data) {

		Assert.notNull(attributeDatas);

		this.attributeDatas.put(key, data);
	}

	/**
	 * 添加错误码
	 * 
	 * @param errorCode
	 *            错误码
	 */
	public void putError(long errorCode) {

		Assert.notNull(this.attributeDatas);

		setSuccessful(false);

		this.attributeDatas.put(ERROR, ErrorVo.instance(errorCode));
	}

	/**
	 * 返回错误
	 * 
	 * @param errorCode
	 *            错误码
	 */
	public void sendError(long errorCode) {

		setSuccessful(false);

		putError(errorCode);

		sendJsonData();

	}

	/**
	 * 返回JSOn
	 * 
	 * @param response
	 *            响应
	 * @param attributeDatas
	 *            返回结果
	 */
	public static void sendJsonData(HttpServletResponse response, Object attributeDatas) {
		sendJsonData(response, attributeDatas, defContentType);
	}

	/**
	 * 返回JSON
	 * 
	 * @param response
	 *            响应
	 * @param attributeDatas
	 *            返回结果
	 * @param contentType
	 */
	public static void sendJsonData(HttpServletResponse response, Object attributeDatas, String contentType) {

		Assert.notNull(response);

		Assert.notNull(attributeDatas);

		response.setContentType(contentType);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String resultJsonStr = new JSONSerializer().exclude("*.class").include("*").serialize(attributeDatas);

			out.println(resultJsonStr);

		} catch (IOException ex1) {

			LogFactory.getInstance(AjaxJsonResponse.class).logRecord(LogConstant.ERROR, ex1.toString());

			ex1.printStackTrace();
		} finally {

			if (out != null) {
				out.close();
			}
		}

	}

	/**
	 * 返回JSON
	 */
	public void sendJsonData() {
		sendJsonData(this.response, this.attributeDatas, this.contentType);

	}

	/**
	 * 返回带contentType格式的Json
	 * 
	 * @param contentType
	 *            类似"text/html;charset=utf-8"
	 */
	public void sendJsonData(String contentType) {
		sendJsonData(this.response, this.attributeDatas, contentType);
	}

	/**
	 * @return the response
	 */
	public HttpServletResponse getResponse() {
		return response;
	}

	/**
	 * @param response
	 *            the response to set
	 */
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	/**
	 * @return the attributeDatas
	 */
	public Map<String, Object> getAttributeDatas() {
		return attributeDatas;
	}

	/**
	 * @param attributeDatas
	 *            the attributeDatas to set
	 */
	public void setAttributeDatas(Map<String, Object> attributeDatas) {
		this.attributeDatas = attributeDatas;
	}

}
